package com.musifind.activities;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.fizy.client.controllers.FizyControllers;
import com.fizy.client.objects.Album;
import com.musifind.adapters.SearchAdapterAlbums;
import com.musifind.common.DateTimeUtils;
import com.musifind.models.SearchItems;

public class AlbumsOfArtistActivity extends Activity {

	private SearchAdapterAlbums searchAdapterAlbums;
	private ArrayList<SearchItems> searchArrayAlbums;
	private ListView lstSearchAlbums;
	private ImageView imgLogo;
	private String artistName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_albums_of_artist);

		imgLogo = (ImageView) findViewById(R.id.imgBlankPage);

		searchArrayAlbums = new ArrayList<SearchItems>();
		searchAdapterAlbums = new SearchAdapterAlbums(AlbumsOfArtistActivity.this,
				R.layout.search_list_items_row_album, searchArrayAlbums);
		lstSearchAlbums = (ListView) findViewById(R.id.lstSearchAlbumItems);
		lstSearchAlbums.setAdapter(searchAdapterAlbums);

		lstSearchAlbums.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				SearchItems item = (SearchItems) searchAdapterAlbums.getItem(position);
				Intent i = new Intent(AlbumsOfArtistActivity.this, TracksOfAlbumActivity.class);
				i.putExtra("albumId", item.getAlbumId());
				i.putExtra("albumName", item.getAlbumName());
				startActivity(i);
			}
		});

		bundleDatas();
	}

	public void getAlbumResults(final String artistId) {
		if (artistId != null && !artistId.isEmpty()) {
			runOnUiThread(new Runnable() {
				public void run() {
					FizyControllers controllers = new FizyControllers();
					List<Album> albums = controllers.getArtistAlbums(artistId);
					for (Album album : albums) {
						String strAlbumCover = album.getCover_medium();
						String strAlbumName = album.getName();
						String strAlbumYear = album.getRelease_date();
						String strAlbumYearNew = null;
						try {
							strAlbumYearNew = DateTimeUtils.stringToDate(strAlbumYear);
						} catch (ParseException e) {
							Log.e("Date Conversion Problem", "Date hasn't parsed");
						}
						searchArrayAlbums.add(new SearchItems(album.getId(), strAlbumCover, strAlbumName, " ("
								+ strAlbumYearNew + ")", artistName));
					}
					searchAdapterAlbums.notifyDataSetChanged();
					if (searchArrayAlbums != null && searchArrayAlbums.size() > 0) {
						imgLogo.setVisibility(View.INVISIBLE);
					}
				}
			});
		}
	}

	public void bundleDatas() {
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			String artistId = String.valueOf(bundle.getInt("artistId"));
			artistName = bundle.getString("artistName");
			setTitle(artistName + " albümleri");
			getAlbumResults(artistId);
		}
	}
}
